package com.nd.mobile.study.view;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.domain.AttentionCatalog;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;

public class AttentionCatalogActivity extends BaseActivity {
	private ListView		mLV;
	private AttentionCatalogAdapter	mAdapter;
	private AttentionCatalog[]		mAttentionCatalog;
	private Intent			mIntent2Go;
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.attention_info);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		getAttentionInfo();
	}

	private void getAttentionInfo() {
		mLoading.p();
		new GetAttentionInfoTask(this).execute();
	}

	@Override
	protected void setFieldValues() {
		mIntent2Go = new Intent().setClass(this, AttentionDetailActivity.class);
	}

	@Override
	protected void initViews() {
		super.initHeader();
		mHeaderTitleTv.setText(R.string.tv_app_recentnotify);
		mHeaderBtnHome.setVisibility(View.VISIBLE);
		mLV = (ListView) findViewById(R.id.common_lv);
	}

	private class GetAttentionInfoTask extends SimpleAsyncTask<Void, Void, AttentionCatalog[]>{

		public GetAttentionInfoTask(Context context) {
			super(context);
		}

		@Override
		protected AttentionCatalog[] onLoad(Void... params) throws Exception {
			return App.getInstance().getUser().getAttentionInfo();
		}

		@Override
		protected void onUI(AttentionCatalog[] result) {
			mAttentionCatalog = result;
			mAdapter = new AttentionCatalogAdapter(mContext, mAttentionCatalog);
			mLV.setAdapter(mAdapter);
			mLoading.v();
			mLV.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					AttentionCatalogAdapter adapter = (AttentionCatalogAdapter) parent.getAdapter();
					mIntent2Go.putExtra(BundleConstants.ATTENTION_INFO, adapter.getData().get(position));
					startActivity(mIntent2Go);
				}
			});
		}
		
	}
	
	private class AttentionCatalogAdapter extends SimpleListAdapter<AttentionCatalog>{

		public AttentionCatalogAdapter(Context context, AttentionCatalog[] data) {
			super(context, data);
		}

		public AttentionCatalogAdapter(Context context, List<AttentionCatalog> data) {
			super(context, data);
		}
		
		private class ViewHolder {
			TextView	tvName;
			TextView	tvDate;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.attention_list_item, null);

				holder = new ViewHolder();
				holder.tvName = (TextView) convertView.findViewById(R.id.attention_list_tv_name);
				holder.tvDate = (TextView) convertView.findViewById(R.id.attention_list_tv_date);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvName.setText(mData.get(position).getTitle());
			holder.tvDate.setText(mData.get(position).getAddTime());
			return convertView;
		}
		
	}
}
